﻿<?php
require '../Controller/Config.php';
require_once '../Controller/ConnectFB.php';

//if(isset($_SESSION['ACCESS_PAGE_ID'])){
//    if($_SESSION['ACCESS_PAGE_ID']!=$_GET['sid']){
//        echo "<script>
//        alert('โปรดตรวจสอบเงื่อนไขก่อนตอบแบบสอบถาม');
//        window.location.href = '../Interface/MenuHome.php';
//        </script>";
//    }
//}else{
//    echo "<script>
//    alert('โปรดตรวจสอบเงื่อนไขก่อนตอบแบบสอบถาม');
//    window.location.href = '../Interface/MenuHome.php';
//    </script>";
//}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<head>
<title><?php echo $_GET['poll'] ?></title>
<link rel="stylesheet" href="../jquery/css/start/jquery-ui-1.10.1.custom.css" type="text/css" />

</head>
<style>
    body{
    	background-image:url('../images/bg.jpg');
        background-repeat:repeat-x;
        background-color:#f8e3b7;
    }
</style>
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Loader" type="text/javascript"></script>
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<script>
    $(function() {
            $( "#btnSubmit" )
              .button()
              .click(function( event ) {
//                event.preventDefault();
              });
        });
        
    function useNumOnly(v){
    
    if(v.value!=''){
        if(isNaN(v.value)){
            alert('กรอกข้อมูลเฉพาะตัวเลข!!');
            v.value = '';
        }
    }
}    
</script>
<?php

$userId = $fb_userData['id'];
//$userId = 1008;

$q = "SELECT DISTINCT
        poll.created_by,
        poll.poll_id,
        poll.poll_title
        FROM
        poll WHERE created_by <> '".$userId."' AND poll.poll_id 
        NOT IN(SELECT answer.poll_id FROM answer WHERE answer.user_id = '".$userId."' 
        GROUP BY poll_id) AND poll.poll_id = ".$_GET['pollId']."";
//echo $q;
$rs = mysql_query($q) or die("Error SQL[ ".$q." ] ".mysql_error());
$rowCount = mysql_num_rows($rs);
mysql_free_result($rs);


/*
 * Check Condition
 */
$save_disable = "";
if($rowCount == 0){
    $save_disable = "disabled";
}else{
    $save_disable = "";
}

/*
 * Save form
 */
if(isset($userId) && isset($_GET['pollId'])){
    if(($userId!=NULL && $userId!="") || ($_GET['pollId']!=NULL && $_GET['pollId']!="")){

        $pollId = $_GET['pollId'];

        $query_poll = "SELECT * FROM poll WHERE poll_id = ".$pollId;
        $rs1 = mysql_query($query_poll) or die("Error SQL[ ".$query_poll." ] ".mysql_error());

        echo <<<HTML
            <form name="pollFrm" id="pollFrm" method="post" action="../Controller/SaveAnswerAction.php" >
HTML;

        $i=0;
        $inputType = "";
        $arrData = array();
        $arrScale = array();

        //fetch poll by created_by AND poll_id
        while($obj1 = mysql_fetch_array($rs1)){


            echo "<b>ชื่อแบบสอบถาม : ".$obj1['poll_title']."</b>";
            echo "<br />";
            echo "<b>รายละเอียดเกียวกับแบบสอบถาม : ".$obj1['poll_desc']."</b>";
            $pollDetial = $obj1['poll_desc'];
            echo "<br /><br />"; 

            $arrData['poll_title'] = $obj1['poll_title'];
            $arrData['poll_desc'] = $obj1['poll_desc'];

            $query_question = "SELECT * FROM question WHERE poll_id = ".$obj1['poll_id'];
            $rs2 = mysql_query($query_question) or die("Error SQL[ ".$query_question." ] ".mysql_error());

            //fetch question by poll_id
            while($obj2 = mysql_fetch_array($rs2)){

                echo '<input type="hidden" name="questionId[]" value="'.$obj2['question_id'].'" />';
                echo "<b>".($i+1).". ".$obj2['question_value']."</b>";
                echo "<br />";


                if($obj2['question_type']==1){
                    $inputType = '<input type="radio" name="data'.$i.'[]" />';
                }else if($obj2['question_type']==2){
                    $inputType = '<input type="checkbox" name="data'.$i.'[]" />';
                }else if($obj2['question_type']==3){
                    $qchkcomstat = "SELECT cal_stat AS stat FROM
                                    compute where poll_id = '".$obj1['poll_id']."' and question_id = '".$obj2['question_id']."'";
                    
                    $compute = mysql_query($qchkcomstat) or die("Error SQL[ ".$qchkcomstat." ] ".mysql_error());
                    $getcompute = mysql_fetch_array($compute);
//                    echo $qchkcomstat."<br/>";
//                    echo "Stat : ".$getcompute['stat'];
                    if($getcompute['stat']==1){
                        $inputType = '<input type"text" onKeyUp="useNumOnly(this)" placeholder="กรอกข้อมูลเฉพาะตัวเลข" name="data'.$i.'[]" ></textarea>';
                    }else{
                        $inputType = '<textarea cols="30" rows="5" name="data'.$i.'[]" ></textarea>';
                    }
                    
                    
                }else if($obj2['question_type']==4){
                    $inputType = 'scale';
                    $inputTypeScale = '';
                }


                $query_choice = "SELECT * FROM choice WHERE question_id = ".$obj2['question_id']." AND poll_id = ".$obj1['poll_id'];
                $rs3 = mysql_query($query_choice) or die("Error SQL[ ".$query_choice." ] ".mysql_error());
                $j=0;
                $k=0;

                $arrScale = array();
                $arrScaleLabel = array();

                //fetch choice by question_id AND poll_id
                while($obj3 = mysql_fetch_array($rs3)){

                    if($inputType!="scale"){
                        if($obj2['question_type']==3){
                            echo $inputType;
                            echo "<br />";

                            $arrData[$i][$obj2['question_value']][$k] = $inputType;
                            $k++;
                        }else{

                                if($obj2['question_type']==1){
                                        $inputType = '<input type="radio" name="data'.$i.'[]" value="'.$obj3['choice_value'].'" />';
                                }else if($obj2['question_type']==2){
                                        $inputType = '<input type="checkbox" name="data'.$i.'[]" value="'.$obj3['choice_value'].'" />';
                                }

                            echo $inputType." ".$obj3['choice_value'];
                            echo "<br />";

                            $arrData[$i][$obj2['question_value']][$k] = $inputType." ".$obj3['choice_value'];
                            $k++;
                        }
                    }else{

                        $arrScale[$j] = $obj3['choice_value'];


                        $query_option = "SELECT * FROM option_scale WHERE choice_id = ".$obj3['choice_id'];
                        $rs4 = mysql_query($query_option) or die("Error SQL[ ".$query_option." ] ".mysql_error());

                        //fetch option_scale by choice_id
                        while($obj4 = mysql_fetch_array($rs4)){

                                $arrScaleLabel[$j] = $obj4['option_value'];

                                $arrData[$i][$obj2['question_value']][$obj3['choice_value']] = $obj4['option_value'];

                        }

                        if($j==1){

                                for($loop = $arrScale[0]; $loop <= $arrScale[1]; $loop++){
                                        $inputTypeScale .= '<input type="radio" name="data'.$i.'[]" value="'.$loop.'" />';
                                }
                                echo $arrScaleLabel[0]." [".$arrScale[0]."] ".$inputTypeScale." [".$arrScale[1]."] ".$arrScaleLabel[1];
                                echo "<br />";
                        }

                        $j++;
                    }              
                }
                echo "<br />";
                $i++;
            }    
        }
        mysql_close();
        echo "<br />";
        
        $title=urlencode($_GET['poll']);
        $url=urlencode("http://".$_SERVER['HTTP_HOST']."/project/Interface/MenuAnswer.php?page=CheckCondition&pollId=".$_GET['pollId']."&poll=".$_GET['poll']);
        $summary=urlencode($pollDetial);
        $image=urlencode($_SERVER['HTTP_HOST']."/project/images/headlogo.png");
//        $pathLink = $_SERVER['HTTP_HOST']."/project/Interface/MenuAnswer.php?page=CheckCondition&pollId=".$_GET['pollId']."&poll=".$_GET['poll'];
        
        
        $getOCCU = (isset($_GET['occu'])==true)? $_GET['occu'] : 0;
        $getCTRY = (isset($_GET['country'])==true)? $_GET['country'] : 0;
        $getSEXS = (isset($_GET['sex'])==true)? $_GET['sex'] : 0;
        $getAGES = (isset($_GET['age'])==true)? $_GET['age'] : 0;
        
        echo <<<HTML
            <input type="hidden" name="userId" value="{$userId}" />
            <input type="hidden" name="pollId" value="{$_GET['pollId']}" />
            <input type="hidden" name="poll" value="{$_GET['poll']}" />
            <input type="hidden" name="first_name" value="{$fb_userData['first_name']}" />
            <input type="hidden" name="last_name" value="{$fb_userData['last_name']}" />
            <input type="hidden" name="occu" value="{$getOCCU}" />
            <input type="hidden" name="country" value="{$getCTRY}" />
            <input type="hidden" name="sex" value="{$getSEXS}" />
            <input type="hidden" name="age" value="{$getAGES}" />
            
         <table>
            <tr>
                <td>
                    <input type="submit" name="btnSubmit" id="btnSubmit" value="เสร็จสิ้นการตอบแบบสอบถาม" {$save_disable} />         
                </td>
                <td>
                    <a onClick="window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=$title&amp;p[summary]=$summary&amp;p[url]=$url&amp;&amp;p[images][0]=$image','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)"><image src="../icon/facebook.png" style="width:80px"></a>
                </td>
            </tr>
         </table>
   </form>
HTML;


        // echo "<pre>";
        // print_r($arrData);
        // echo "</pre>";

    }
}else{
    echo "Please login to access this";
}
?> 